from selenium import webdriver
from selenium.webdriver.common.by import By
import time


class Ecshop:
    def __init__(self):
        # 打开游览器
        self.driver = webdriver.Chrome()
        # 打开ecshop
        self.driver.get('https://ecshop.test2.shopex123.com')
        self.driver.maximize_window()

    def register(self):
        # 定位到注册按钮并点击
        self.driver.find_element(By.CLASS_NAME,'reg').click()
        # 定位到用户名输入框并输入用户名
        time.sleep(2)
        self.driver.find_element(By.ID,'username').send_keys('测试看看')
        # 定位到邮箱输入框并输入邮箱
        self.driver.find_element(By.ID,'email').send_keys('ly13437183230@outlook.com')
        # 定位到密码输入框输入密码
        self.driver.find_element(By.ID,'password1').send_keys('ly123456')
        # 定位到再次输入密码
        self.driver.find_element(By.ID,'confirm_password').send_keys('ly123456')
        # 定位到同意并注册按钮并点击
        time.sleep(2)
        self.driver.find_element(By.CLASS_NAME,'signup_button').click()


    def login(self):
        # 定位到登录按钮并点击
        self.driver.find_element(By.CLASS_NAME ,'sign').click()
        # 定位到用户名输入框并输入
        time.sleep(2)
        self.driver.find_element(By.NAME,'username').send_keys('测试看看')
        # 定位到密码输入框并输入
        time.sleep(2)
        self.driver.find_element(By.NAME, 'password').send_keys('ly123456')
        # 定位到登录按钮并点击
        self.driver.find_element(By.NAME, 'submit').click()


    def search(self,keyword):
        # 定位到搜索框并输入关键字
        self.driver.find_element(By.ID,'keyword').send_keys(keyword)
        # 定位到搜索按钮并点击
        time.sleep(2)
        self.driver.find_element(By.CLASS_NAME,'btn_search').click()

    def quit(self):
        # 关闭游览器
        time.sleep(2)
        self.driver.quit()

if __name__ == '__main__':
    es = Ecshop()
    # es.register()
    # es.login()
    es.search('防晒霜')
    es.quit()